//获取应用实例
const API = require('../../utils/api') //引入api文件
const app = getApp()

Page({
  data: {
    toSharePageShow: false, //赚钱开关
  },
  onLoad: function (query) {
    let that = this
    app.login(userData)
    function userData(data){
      that.setData({
        userInfo:data
      })
    }
  },
  /**
   * 获取手机号
   */
  getPhoneNumber:function(e){
    let that = this
    wx.login({
      success (res) {
        if (res.code) {
          API.get_mobile({
            appid:app.globalData.appid,
            encryptedData:e.detail.encryptedData,
            iv:e.detail.iv,
            code:res.code
          }).then( res => {
            if( res.code == 200 ){
              that.setData({
                phoneNumber: res.data.phoneNumber
              })
            }else{
              wx.showToast({
                title: '授权登陆失败，请稍后重试',
                icon:'none'
              })
            }
          })
        }
      }
    })
  },
  /**
   * 登陆
   */
  login:function(e){
    // console.log(e.detail)
    let that = this
    app.getUserInfo( getUserInfo, this.data.phoneNumber )
    function getUserInfo( data ){
      var userInfo = data
      console.log('回调函数',data)
      that.setData({
        userInfo:data
      })
    }
  },
  /**
   * 统一路由
   */
  routeCase: function () {
    if( !this.data.userInfo ){
      wx.showToast({
        title: '请点击下方按钮登陆',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/index/set-case/set-case',
    })
  },
})